package com.mk.interceptor.header;

import act.controller.Controller;
import com.mk.domain.CpUser;
import com.mk.interceptor.PermissionInterface;
import com.mk.util.Redis;
import com.mk.util.S2;
import org.osgl.util.S;


@SuppressWarnings("unused")
@Deprecated
public class HeaderService extends Controller.Base implements PermissionInterface {
    @HeaderLoginUser
    protected CpUser me;

    @Override public CpUser me() {
        return me;
    }

    public String genKey(String key) {
        if (me != null) {
            key = me.getDevid() + "-" + key;
        }
        return key;
    }

    @Override public void login(CpUser user) {
        if (S.isBlank(user.getDevid())) {
            if (me != null && S.isNotBlank(me.getDevid())) {
                user.setDevid(me.getDevid());
            } else {
                user.setDevid(S2.UUID());
            }
        }
        me = user;
        Redis.userDb.getBucket(user.getDevid(), CpUser.class).set(user);
        me = user;
    }

    @Override public void logout() {
        if (me != null) {
            Redis.userDb.getBucket(me.getDevid(), CpUser.class).deleteAsync();
        }
    }
}
